<?php

use Hanson\Vbot\Message\Text;

require_once __DIR__ . '/../inc/Wechat.php';
require_once __DIR__ . '/../inc/TuringApi.php';
require_once __DIR__ . '/../inc/Group.php';
require_once __DIR__ . '/../inc/Group42gou.php';
require_once __DIR__ . '/../inc/GroupTmp.php';
/***************************/

class Li2Niu extends Wechat{
    private $_groupList = []; //开启群聊的群组对象

    function __construct($groupList = [])
    {
        $this->_groupList = $groupList;
        parent::__construct();
    }


    function dealMsg($message)
    {
        parent::dealMsg($message);
        if($this->isSBChatMe()){$this->chatWithSB();}
        elseif($this->isRequest()){$this->addFriend();}

        foreach ($this->_groupList as $group){
            if($this->isHisCase($group)){$group->dealCase($message);return;}
        }
        if($this->isNewGroup()){$this->addNewGroup();}

    }

    private function isHisCase($group)
    {
        if($this->message['from']['UserName']==$group->username){
            return true;
        }else{
            return false;
        }
    }

    private function isNewGroup()
    {
        if($this->message['fromType']=='Group'){
            return true;
        }else{
            return false;
        }
    }

    private function addNewGroup()
    {
        $group = new Group($this->message['from']['NickName']);
        $group->username = $this->message['from']['UserName'];
        $this->_groupList[] = $group;
    }

    /*************私人假聊**************/
    /**
     * 判断是否朋友发来消息
     * @return bool
     */
    protected function isSBChatMe()
    {
        if($this->message['type']=='text'
        and $this->message['fromType']=='Friend'){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 处理朋友发来的文字对话
     */
    protected function chatWithSB()
    {
        TuringApi::getObj()->setPost($this->message['content'],substr($this->message['from']['UserName'],2,32));
        $re_ocntent = TuringApi::getObj()->sent();
        Text::send($this->message['from']['UserName'],$re_ocntent);
    }

    /*************自动加好友**************/
    /**
     * 是否好友请求
     */
    protected function isRequest()
    {
        if ($this->message['type'] === 'request_friend') {
            return true;
        }else{
            return false;
        }
    }

    protected function addFriend()
    {
        vbot('friends')->approve($this->message);
    }


    /*************初始化**************/
    function initConfig()
    {
        parent::initConfig();
        $groupsVbot = vbot('groups');
        foreach ($this->_groupList as$key => $group){
            $group->username = $groupsVbot->getUsernameByNickname($group->nickname,true);
            if(empty($group->username)){
                echo '
'.$group->nickname.' 获取失败!
';
                unset($this->_groupList[$key]);
            }else{
                echo '
'.$group->nickname.' 获取成功:
'.$group->username.'
';
            }
        }
    }



}

$group = [
    // new Group('全都是沙雕'),
    // new Group('傻屌之家'),
    // new Group42gou('沙雕斗图尬聊'),
//    new Group('快乐泡泡沙雕群'),
//	new Group('进来嘤嘤嘤'),
//    new Group('沙雕网友欢乐多'),
//	new Group('寝室群'),
//	new GroupTmp('肥宅快乐屋')

];

$a = new Li2Niu($group);
$a->vbot->server->serve();